Loader Class
The Loader loads and parses different game assets, such as sounds, textures, TMX World files (exported from the Tiled Editor), and Sprite Atlas files (published from Texture Packer).
Constructor
Item Index
Methods
Methods
_dataget
-
file -
[callback]
Gets a file's data via ajax.
Parameters:
-
fileObjectThe file descriptor object
-
[callback]Function optionalThe callback to call once the file has loaded.
fileDoneorfileErrorwill be called for you.
_getFormatAjaxType
-
type
Returns the ajax type that represents each format type
Parameters:
-
typeATLAS_FORMAT | FILE_FORMATThe format to get an ajax type for
_loadJsonTilesets
-
file
Loads the tilesets found in a JSON formatted tilemap object.
Parameters:
-
fileObjectThe file descriptor object
_loadXmlTilesets
-
file
Loads the tilesets found in a XML formatted tilemap object.
Parameters:
-
fileObjectThe file descriptor object
_onTilesetError
-
file -
error
Called each time a tileset has an error when loading.
Parameters:
-
fileObjectThe file descriptor object.
-
errorMixedThe error thrown when loading.
_onTilesetLoaded
-
file
Called each time a tileset is loaded successfully.
Parameters:
-
fileObjectThe file descriptor object.
add
-
type -
key -
url -
[options]
Adds an asset to be loaded
Parameters:
-
typeStringThe type of asset ot load (image, spritesheet, textureatlas, bitmapfont, tilemap, tileset, audio, etc)
-
keyStringThe unique key of the asset to identify it
-
urlStringThe URL to load the resource from
-
[options]Object optionalExtra options to apply to the asset, different asset types may require extra options
-
[crossOrigin=false]Boolean optionalTrue if an image load should be treated as crossOrigin
-
Returns:
Returns itself.
audio
-
key -
urls -
[overwrite=false]
Add an audio file to the Loader.
Parameters:
-
keyStringUnique asset key of this image file.
-
urlsArrayURLs of audio files.
-
[overwrite=false]Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
bitmapFont
-
key -
textureURL -
[dataUrl] -
[data] -
[format=FILE_FORMAT.XML] -
[overwrite=false]
Add a bitmap font to the Loader.
Parameters:
-
keyStringUnique asset key of the bitmap font.
-
textureURLStringThe url of the font image file.
-
[dataUrl]String optionalThe url of the font data file (xml/fnt)
-
[data]Object optionalAn optional XML data object (to use instead of loading from a URL)
-
[format=FILE_FORMAT.XML]FILE_FORMAT optionalThe format of the bitmap font data.
-
[overwrite=false]Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
emit
-
type -
data
Emits an event which will run all registered listeners for the event type
Parameters:
-
typeStringThe event name to emit
-
dataMixedAny data you want passed along with the event
Returns:
Returns itself.
fileComplete
-
key
Called when a file is successfully loaded.
Parameters:
-
keyStringKey of the successfully loaded file.
fileDone
-
key -
error
Called when a file is done (error or loaded)
Parameters:
-
keyStringKey of the file done
-
errorMixedThe error that occurred (if there was one)
fileError
-
key -
error
Error occured when load a file.
Parameters:
-
keyStringKey of the error loading file.
-
errorMixedThe error that was thrown.
getAudioUrl
-
urls
Chooses the audio url to use based on browser support.
Parameters:
-
urlsArrayAn array of URLs to choose from, chooses the first in the array to be supported by the browser.
Returns:
Returns the URL that was chosen, or undefined if none are supported.
hasKey
-
key
Check whether asset exists with a specific key.
Parameters:
-
keyStringKey of the asset you want to check.
Returns:
Return true if exists, otherwise return false.
image
-
key -
url -
[overwrite=false]
Add an image to the Loader.
Parameters:
-
keyStringUnique asset key of this image file.
-
urlStringURL of image file.
-
[overwrite=false]Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
loadFile
()
Loader
chainable
Loads a single asset from the queued assets in this Loader. To load a single file first queue it by using
one of the methods named for an asset (like audio, image, tilemap, etc.), then call this to load the
first in the queue.
Note: To load the entire queue at once use start.
Returns:
Returns itself.
off
-
type -
listener
Removes a listener function for an event type
Parameters:
-
typeStringThe event name to emit
-
listenerFunctionThe function to remove
Returns:
Returns itself.
on
-
type -
listener
Registers a listener function to be run on an event occurance
Parameters:
-
typeStringThe event name to listen for
-
listenerFunctionThe function to execute when the event happens
Returns:
Returns itself.
once
-
type -
listener
Registers a one-time callback for an event
Parameters:
-
typeStringThe event name to listen for
-
listenerFunctionthe callback to call when the event occurs
Returns:
Returns itself.
reset
()
Loader
chainable
Reset loader, this will remove all loaded assets from the loader's stored list (but not from the cache).
Returns:
Returns itself.
spritesheet
-
key -
url -
frameWidth -
frameHeight -
numFrames -
[overwrite=false]
Add a sprite sheet image to the Loader.
Parameters:
-
keyStringUnique asset key of this image file.
-
urlStringURL of image file.
-
frameWidthNumberWidth of each single frame.
-
frameHeightNumberHeight of each single frame.
-
numFramesNumberHow many frames in this sprite sheet.
-
[overwrite=false]Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
start
()
Loader
chainable
Starts the loading of all the assets that are queued to load
Returns:
Returns itself.
text
-
key -
url -
[overwrite=false]
Add a text file to the Loader.
Parameters:
-
keyStringUnique asset key of this image file.
-
urlStringURL of image file.
-
[overwrite=false]Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
tilemap
-
key -
url -
[data] -
[format=FILE_FORMAT.JSON] -
[overwrite=false]
Add a tilemap to the Loader.
Parameters:
-
keyStringUnique asset key of the tilemap data.
-
urlStringThe url of the map data file (csv/json/xml)
-
[data]String | Object optionalThe data for the map, (to use instead of loading from a URL)
-
[format=FILE_FORMAT.JSON]Number optionalThe format of the map data.
-
[overwrite=false]Boolean optionalIf an entry with a matching key already exists this will over-write it.
Returns:
Returns itself.
Properties
assets
Array
The array of asset keys
assets
Array
The asset data
baseUrl
String
The base URL to prepend to a url, requires the trailing slash
crossOrigin
String
The cross origin value for loading images
done
Number
Number of assets done to load (for progress)
isLoading
Boolean
Whether the loader is actively loading the assets
isLoading
Boolean
Whether the loader has finished loading
progress
Number
The progress of the loader (0 - 100)
total
Number
Number of assets total to load
Events
complete
Fired when all the assets have loaded
error
Fired if a loader encounters an error
Event Payload:
-
errorMixedThe error that occured when loading
-
keyStringThe key for the asset that was being loaded
progress
Fired when an item has loaded
Event Payload:
-
progressNumberThe integer progress value, between 0 and 100.
start
Fired when an item has started loading
Event Payload:
-
numAssetsNumberThe number of assets that are going to be loaded